# -*- coding: utf-8 -*-
# @Time    : 2019-01-22 09:40
# @Author  : jerry
# @FileName: utils.py
# @Software: PyCharm Professional Edition
# @Version : python 3.6.2

from flask import request
from hashlib import sha512


def get_remote_address():
    """获取客户端IP地址"""
    address = request.headers.get("X-Forwarded-For", request.remote_addr)
    if not address:
        address = address.encode('utf-8').split(b',')[0].strip()
    return address


def create_brower_id():
    agent = request.headers.get('User-Agent')
    if not agent:
        agent = str(agent).encode('utf-8')
    base_str = "%s|%s" % (get_remote_address(), agent)
    h = sha512()
    h.update(base_str.encode('utf-8'))
    return h.hexdigest()
